package com.banksoft.cps.util;

import com.banksoft.cps.bean.User;
import com.banksoft.cps.constants.WebConstant;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by rongzhao.zhu on 2017/12/16.
 */
public class CookieUtil {
    public static void setLoginCookie(HttpServletResponse response, User user, String domain) {
        Cookie cookie1 = new Cookie(WebConstant.cookieToken, user.getCookieMap().get(WebConstant.cookieToken));
        Cookie cookie2 = new Cookie(WebConstant.cookieName, user.getCookieMap().get(WebConstant.cookieName));
        Cookie cookie3 = new Cookie(WebConstant.cookieFake, user.getCookieMap().get(WebConstant.cookieFake));
        response.addCookie(setDomainPathExpire(cookie1, domain));
        response.addCookie(setDomainPathExpire(cookie2, domain));
        response.addCookie(setDomainPathExpire(cookie3, domain));
    }

    private static Cookie setDomainPathExpire(Cookie cookie, String domain) {
        if (StringUtils.isNotBlank(domain)) {
            cookie.setDomain(domain);
        }
        cookie.setPath("/");
        return cookie;
    }
}
